home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / KEYBOARD.SWG / 0074_Multiple Key Input.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-03  |  8KB  |  179 lines

  1. (*
  2. >Can someone help me out?  I need to able to read keyboard input like
  3. >having the spacebar helddown then having the Right arrow used?
  4.  
  5. HOW TO ENABLE YOUR KEYBOARD TO READ MULTIPLE KEYS SIMULTANEOUSLY
  6.  
  7. - by Lou DuChez
  8.  
  9. Manuals will tell you that you can read port 60h to determine if a
  10. key is being pressed/released.  What they don't tell you is, the
  11. keyboard interrupt (09h) will instantly process the port and reset
  12. it to zero as soon as any keyboard activity occurs; so by the time you
  13. get to look at it, port 60h invariably contains no data for you to use.
  14. To use port 60h, then, you'll need to alter the keyboard interrupt so
  15. that the port's data gets stored to variables you can use.
  16.  
  17. The routine in this post will let you read multiple keys simultaneously.
  18. I will do this by making an array [0..127] of boolean called KeyDown:
  19. it records the status of each key on your keyboard with "true" meaning
  20. "down."  The lower seven bits of port 60h tell you which key is being
  21. reported on (i.e., the "scan code" of the key); the high bit indicates a
  22. "press" if it's 0 and a "release" it it's 1.  So when you press or
  23. release a key, my new keyboard interrupt routine will determine which key
  24. is being altered, and set the proper element of KeyDown to the right value.
  25.  
  26. You will probably want to compile your program as "far" ({$F+}), or make
  27. a unit out of these routines.  You will need these global variable
  28. declarations:
  29. ______
  30.  
  31. *)
  32. var keydown: array[0..127] of boolean;   { see the above }
  33.     oldkbdint: procedure;       { points to the "normal" keyboard handler }
  34. (*
  35. ______
  36.  
  37. Next, put these lines of code in your program, maybe in the "main" part:
  38. ______
  39.  
  40. *)
  41. getintvec($09, @oldkbdint);        { record location of old keyboard int }
  42. setintvec($09, @newkbdint);        { this line installs the new interrupt }
  43. (*
  44. ______
  45.  
  46. We need to recall the location of the "normal" keyboard handler for two
  47. reasons: because we'll need to restore it when the program terminates, and
  48. because the "new" handler will need to call it.  (The "old" handler performs
  49. certain housekeeping duties that we still need.)  This is the new keyboard
  50. interrupt handler:
  51. ______
  52. *)
  53.  
  54. procedure newkbdint; interrupt;
  55. begin
  56.   keydown[port[$60] mod 128] := (port[$60] < 128);  { record current status }
  57.   asm pushf; end;                             { must precede call to old int }
  58.   oldkbdint;                                  { call old interrupt }
  59.   asm cli; end;                               { disable hardware interrupts }
  60.   memw[$0040:$001a] := memw[$0040:$001c];     { clear the keyboard buffer }
  61.   asm sti; end;                               { enable hardware interrupts }
  62.   end;
  63. (*
  64. ______
  65.  
  66. Explanations:
  67.  
  68. The "KeyDown" line checks the indicated key for a press or release.  If
  69. the port is returning something less than 128 (i.e., high bit of "0"),
  70. the key is not down.  The "ASM PUSHF; END" line "pushes" all the
  71. "flags" onto the stack and is necessary before calling the original
  72. interrupt routine on the next line.  The "ASM CLI; END" line performs
  73. an assembler instruction to prevent hardware interrupts just long
  74. enough for the next line to clear the keyboard buffer; then the "ASM
  75. STI; END" line re-enables hardware interrupts.  (About the keyboard
  76. buffer: it's a ring buffer, where a block of usually 32 bytes is used
  77. to store unprocessed keystrokes.  It's a "ring" because there are
  78. pointers to the "first unread" character and the "last unread"
  79. character.  The "Memw" line sets the pointers equal to each other, thus
  80. "clearing" the buffer.)
  81.  
  82. Finally, when you're done with your new keyboard interrupt, restore the
  83. "normal" keyboard handler with this line:
  84. *)
  85. setintvec($09, @oldkbdint);
  86. (*
  87. And that should do it.  See the next message for the complete program ...
  88.  
  89. HOW TO ENABLE YOUR KEYBOARD TO READ MULTIPLE KEYS SIMULTANEOUSLY
  90.  
  91. From the top, your program should look like:
  92. ______
  93. *)
  94.  
  95. {$F+}
  96. Program KeyboardThingie;
  97. Uses Dos;                 { needed for all the interrupt stuff }
  98.  
  99. var keydown: array[0..127] of boolean;
  100.     oldkbdint: procedure;
  101.  
  102. procedure newkbdint; interrupt;
  103. begin
  104.   keydown[port[$60] mod 128] := (port[$60] < 128);  { record current status }
  105.   asm pushf; end;                             { must precede call to old int }
  106.   oldkbdint;                                  { call old interrupt }
  107.   asm cli; end;                               { disable hardware interrupts }
  108.   memw[$0040:$001a] := memw[$0040:$001c];     { clear the keyboard buffer }
  109.   asm sti; end;                               { enable hardware interrupts }
  110.   end;
  111.  
  112. begin { the main program }
  113.   fillchar(keydown, 128, #0);    { sets array to all "false" }
  114.   getintvec($09, @oldkbdint);
  115.   setintvec($09, @newkbdint);
  116.   {
  117.     Put your own code here to actually do something.  The following line of
  118.     code will report all keys that are currently "down"; to use it you'll need
  119.     to declare "i" as a byte variable:
  120.  
  121.     while not keydown[68] do
  122.           for i := 0 to 127 do if keydown[i] then write(i:4);
  123.   }
  124.   setintvec($09, @oldkbdint);
  125.   end.
  126. (*
  127. ______
  128.  
  129. Something to watch out for: this routine does nothing about "Ctrl-Break."
  130. If someone hits "Ctrl-Break" while the alternate keyboard handler is
  131. working, the program will terminate.  Which means that the block of memory
  132. holding the "new" handler will be open to reuse by other programs.  Which
  133. means that your system will crash.  So to prevent this, you should also
  134. make a new "Ctrl-Break" handler.  The approach is much like the above,
  135. but with two differences: the "Ctrl-Break" interrupt is interrupt 1Bh,
  136. and you'll want your new handler to do absolutely nothing.  NOTHING.
  137. As in, no lines of code between "begin" and "end."
  138.  
  139. Finally, to use all this, you'll need to know the "scan codes".  Notice
  140. that a typical key can generate two different characters (like "1" and
  141. "!"); the two characters have the same scan code because the same key
  142. produces both.  Here are the scan codes:
  143. ______
  144.  
  145. "1" - "=" : $02 - $0D
  146. "Q" - "}" : $10 - $1B
  147. "A" - '"' (the "quote" key) : $1E - $28
  148. "Z" - "?" : $2C - $35
  149. F1 - F10  : $3B - $44
  150.  
  151. "space" :      $39    "~" :         $29     "|" :           $2b
  152. "escape" :     $01    "backspace" : $0E     "control" :     $1D
  153. "left shift" : $2A    "caps lock" : $3A     "scroll lock" : $46
  154. "tab" :        $0F    "enter" :     $1C     "right shift" : $36
  155. "printscreen": $37    "alt" :       $38     "home" :        $47
  156. "up" :         $48    "page up" :   $49     "minus" (pad) : $4A
  157. "left arrow" : $4B    "middle" key: $4C     "rightarrow":   $4D
  158. "plus" (pad) : $4E    "end":        $4F     "down":         $50
  159. "page down" :  $51    "insert" :    $52     "delete" :      $53
  160. "num lock" :   $45    F11 :         $D9     F12 :           $DA
  161. ______
  162.  
  163. Use them however you want.  I tend to set up the non-character codes
  164. (like the arrows and "enter" key) as constants.  For the "character"
  165. codes (like '1' and 'K'), I set up an array called ScanOf: it's an
  166. array[' '..'~'] of byte that I use to get the scan codes of characters
  167. with.  For example, at the start of my unit that contains all this, I
  168. load in ScanOf['3'] with $04, meaning that character '3' corresponds
  169. to scan code $04.  Then, if I need to see if the '3' key is down,
  170. I check:
  171.  
  172. KeyDown[ScanOf['3']]
  173. ^^^^^^^ ^^^^^
  174.    |    converts character to scan code (i.e., which "key")
  175.    +--- checks the specified key
  176.  
  177. "True" means "it's down."  But do what you want.
  178. ---
  179. *)